﻿/*
    Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
    Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html) 
 */


using System;
using System.IO;
using Microsoft.Win32;

namespace IvmpEditor
{
    public static class Marketing
    {
        public const string appPrefix = "ivmpGuiEditor";
        const bool alwaysShowExt = true;

        public static bool checkExtensionRegisteredTo(string extension, string shellOpenCommand)
        {
            try
            {
                var key = Registry.ClassesRoot.OpenSubKey(extension);
                if (key != null)
                {
                    string assockey = key.GetValue(null, null) as string;
                    if (assockey != null)
                    {
                        key = Registry.ClassesRoot.OpenSubKey(assockey);
                        if (key != null)
                        {
                            key = key.OpenSubKey(@"shell\open\command");
                            if (key != null)
                            {
                                var cmd = key.GetValue(null, "null") as string;
                                return cmd.ToLower().Trim().Contains(shellOpenCommand.ToLower().Trim());
                            }
                        }
                    }
                }
            }
            catch { }

            return false;
        }
        public static bool AssociateWithFileTypes()
        {
            try
            {
                var exe = new FileInfo(System.Windows.Forms.Application.ExecutablePath);
                string bin = exe.DirectoryName;
                string distrib = bin + @"\distrib";
                string exename = exe.Name;

                foreach (var ext in SupportedExtensions.List)
                    regExtension(ext.extension, true,
                        ext.title + " file",
                        distrib + @"\" + ext.extension.TrimStart(new char[] { '.' }) + ".ico",
                        exe.FullName);

                return true;
            }
            catch
            {
                return false;
            }
        }

        private static void regShellCmd(RegistryKey key, string cmdName, string text, string command)
        {
            key = key.CreateSubKey(cmdName);
            key.SetValue("", text);
            key.CreateSubKey("command").SetValue("", command);
        }
        private static void regExtension(string extension, bool register, string fileTypeName, string iconFileName, string exeFileName)
        {
            if (register)
            {
                // Registering extension in classes root
                regExtension(extension, false, null, null, null);   // delete associtation first

                var assoc = appPrefix + extension.Replace('.', '_');
                Registry.ClassesRoot.CreateSubKey(extension).SetValue(null, assoc);

                var key = Registry.ClassesRoot.CreateSubKey(assoc);
                key.SetValue(null, fileTypeName);
                key.SetValue("AlwaysShowExt", alwaysShowExt ? 1 : 0);
                if (iconFileName != null && File.Exists(iconFileName))
                    key.CreateSubKey("DefaultIcon").SetValue(null, iconFileName);

                key = key.CreateSubKey("shell");
                regShellCmd(key, "Open", "Open with IV:MP GUI Editor", string.Format("{0} \"%1\"", exeFileName));
            }
            else
            {
                // Removing keys related to this extension

                var key = Registry.ClassesRoot.OpenSubKey(extension);
                if (key == null)
                    return;
                string assockey = key.GetValue(null, null) as string;
                if (assockey != null)
                {
                    Registry.ClassesRoot.DeleteSubKeyTree(assockey);
                }
            }
        }
    }
}
